package LeetCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LC_090_SubsetsII {
    public static void main(String[] args) {

    }

    static class Solution {
        public List<List<Integer>> subsetsWithDup(int[] nums) {
            List<List<Integer>> res = new ArrayList<>();
            if (nums == null || nums.length < 1)
                return res;
            Arrays.sort(nums);
            ArrayList<Integer> list = new ArrayList<>();
            solve(nums, 0, res, list);
            return res;
        }

        private void solve(int[] nums, int start, List<List<Integer>> res, ArrayList<Integer> list) {
            res.add(new ArrayList<>(list));
            if (start >= nums.length)
                return;
            for (int i = start; i < nums.length; i++) {
                if (i > start && nums[i] == nums[i - 1])
                    continue;
                list.add(nums[i]);
                solve(nums, i + 1, res, list);
                list.remove(list.size() - 1);
            }
        }

    }
}
